package ar.com.gep.soccer.frontend.utils;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

import ar.com.cdt.framework.business.dto.BaseDTO;

public class FacesUtil implements Serializable {

	private static final long serialVersionUID = -5853266725885244652L;

	public static Locale getLocale() {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		return facesContext.getViewRoot().getLocale();
	}

	public static ResourceBundle getResourceBundle() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Locale locale = facesContext.getViewRoot().getLocale();
		return ResourceBundle.getBundle("messages", locale);
	}

	@SuppressWarnings("rawtypes")
	public static void removeAllMessages() {
		Iterator messageIterator = FacesContext.getCurrentInstance().getMessages();
		while (messageIterator.hasNext()) {
			messageIterator.remove();
		}
	}

	public static void addParametersSession(String key, Object object) {
		FacesContext context = FacesContext.getCurrentInstance();
		context.getExternalContext().getSessionMap().put(key, object);
	}

	public static void removeParametersSession(String key) {
		FacesContext context = FacesContext.getCurrentInstance();
		context.getExternalContext().getSessionMap().remove(key);
	}

	public static Object getParametersSession(String key) {
		FacesContext context = FacesContext.getCurrentInstance();
		return context.getExternalContext().getSessionMap().get(key);
	}

	public static void addParametersFlash(String key, Object object) {
		FacesContext context = FacesContext.getCurrentInstance();
		context.getExternalContext().getFlash().put(key, object);
	}

	public static Object getParametersFlash(String key) {
		FacesContext context = FacesContext.getCurrentInstance();
		return context.getExternalContext().getFlash().get(key);
	}

	public static void redirectToNavigationCase(String outcome) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
	}

	public static void showFatalMessage(String field, String message) {
		showCommonMessage(field, message, FacesMessage.SEVERITY_FATAL);
	}

	public static void showErrorMessage(String field, String message) {
		showCommonMessage(field, message, FacesMessage.SEVERITY_ERROR);
	}

	public static void showInfoMessage(String field, String message) {
		showCommonMessage(field, message, FacesMessage.SEVERITY_INFO);
	}

	private static void showCommonMessage(String field, String message, Severity severity) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage msg = new FacesMessage();
		msg.setSummary(getResourceBundle().getString(message));
		msg.setSeverity(severity);
		facesContext.addMessage(field, msg);
		facesContext.renderResponse();
	}

	public static void addErrorMsg(String form, String msgError) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage msg = new FacesMessage(getResourceBundle().getString(msgError));
		msg.setSeverity(FacesMessage.SEVERITY_ERROR);
		facesContext.addMessage(form, msg);
		facesContext.renderResponse();
	}

	public static String getMessage(String key) {
		return getResourceBundle().getString(key);
	}

	public static void showInfoMessage(String form, String msgError, String extraParameters) {
		showCommonMessage(form, msgError, extraParameters, FacesMessage.SEVERITY_INFO);
	}

	public static void showErrorMessage(String form, String msgError, String extraParameters) {
		showCommonMessage(form, msgError, extraParameters, FacesMessage.SEVERITY_ERROR);
	}

	public static void showFatalMessage(String form, String msgError, String extraParameters) {
		showCommonMessage(form, msgError, extraParameters, FacesMessage.SEVERITY_FATAL);
	}

	private static void showCommonMessage(String field, String message, String extraParameters, Severity severity) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage msg = new FacesMessage();
		msg.setSummary(getResourceBundle().getString(message).concat(" ").concat(extraParameters));
		msg.setSeverity(severity);
		facesContext.addMessage(field, msg);
		facesContext.renderResponse();
	}

	/**
	 * Devuelve un BaseDTO de una lista de BaseDTO.
	 * 
	 * @param list : {@link List}
	 * @param identifier : {@link Long}
	 * @return {@link BaseDTO}
	 */
	public static BaseDTO getBaseDTOFromList(final List<? extends BaseDTO> list, final Long identifier) {
		for (final Object object : list) {
			final BaseDTO roleDTO = (BaseDTO) object;
			if (roleDTO.getId().equals(identifier)) {
				return roleDTO;
			}
		}
		return null;
	}

}